package ext.mango.ab.po;

import java.util.List;

import mango.functions.FuncString;
import mango.orm.Dao;
import mango.orm.DaoFactory;
import mango.orm.DBSession;
import mango.orm.DBSessionFactory;
import mango.orm.entity.Entity;
import mango.service.ServiceManager;
import mango.web.Info;
import ext.mango.ab.eo.EOABHouseKeeperSpendType;
import ext.mango.ab.so.SOABHouseKeeper2;
import ext.mango.base.Messages;
import ext.mango.base.POBase;

public class HouseKeeper2 extends POBase {
    
    public boolean readcode() {
//        String sql = "select code||'-'||codename spendtype from basecode where codetype='spendtype' order by code";
        String sql = "select concat(spendtype,'-',name) spendtype from ABHouseKeeperSpendType where operator=? or operator='default' order by spendtype"; //$NON-NLS-1$
        List<Entity> list = DaoFactory.c(sql).setParameter(this.getCurrentUser().getId()).listQuery();
        this.setResult(Entity.toJSON(list, "spendtype")); //$NON-NLS-1$
        return true;
    }
    
    public boolean readKeeper(){
        String sql = "select id QUERYID, items, actiondate from ABHouseKeeper2 where operator=@userid "; //$NON-NLS-1$
        String startDate = this.getRequestParameter("STARTDATE"); //$NON-NLS-1$
        String endDate = this.getRequestParameter("ENDDATE"); //$NON-NLS-1$
        if (!FuncString.isBlank(startDate)) {
//            sql += " and to_char(actiondate, 'yyyy-mm-dd')>=@STARTDATE";
            sql += " and actiondate>=@STARTDATE"; //$NON-NLS-1$
        }
        if (!FuncString.isBlank(endDate)) {
//            sql += " and to_char(actiondate, 'yyyy-mm-dd')<=@ENDDATE";
            sql += " and actiondate<=@ENDDATE"; //$NON-NLS-1$
        }
        sql += " order by actiondate desc "; //$NON-NLS-1$
        Dao dao = DaoFactory.c(sql)
                .setParamByName("userid", this.getCurrentUser().getId()) //$NON-NLS-1$
                .setParamByName("STARTDATE", startDate) //$NON-NLS-1$
                .setParamByName("ENDDATE", endDate) //$NON-NLS-1$
                .setStart(this.getRequestInt("start")) //$NON-NLS-1$
                .setLimit(this.getRequestInt("limit")); //$NON-NLS-1$
        int totalCount = dao.getTotalCount();
        this.setResult(Entity.toJSON(dao.listQuery(), "housekeeper", totalCount)); //$NON-NLS-1$
        return true;
    }
    
    public boolean updateOrCreate() {
        String queryId = this.getRequestParameter("QUERYID"); //$NON-NLS-1$
        String olditems = this.getRequestParameter("ITEMS"); //$NON-NLS-1$
        if (FuncString.isBlank(olditems)) {
            this.addInfo(Info.ERROR, Messages.getString(this.getLanguage(), "spendtypecantbenull")); //$NON-NLS-1$
            return false;
        }
        if (FuncString.isBlank(queryId)) {
            // create new
            SOABHouseKeeper2 so = ServiceManager.getService(SOABHouseKeeper2.class);
            so.createHouseKeeper(this);
        }
        else {
            // row edit
            SOABHouseKeeper2 so = ServiceManager.getService(SOABHouseKeeper2.class);
            so.updateHouseKeeper(this);
        }
        return true;
    }
    
    public boolean deleteKeeper(){
        String id = this.getRequestParameter("ID"); //$NON-NLS-1$
        String sql = "delete from ABHouseKeeper2 where id=?"; //$NON-NLS-1$
        String sql2 = "delete from ABHouseKeeperDetail where pid=?"; //$NON-NLS-1$
        DBSession session = DBSessionFactory.getSession();
        session.execute(sql, id);
        session.execute(sql2, id);
        session.commit();
        return true;
    }
    
    public boolean readSpendType(){
        String sql = "select spendtype querycode, spendtype code,name codename from ABHouseKeeperSpendType where operator=? or operator='default' order by spendtype"; //$NON-NLS-1$
        this.setResult(Entity.toJSON(DaoFactory.c(sql).setParameter(this.getCurrentUser().getId()).listQuery(), "spendtype")); //$NON-NLS-1$
        return true;
    }
    
    public boolean updateOrCreateSpendType(){
        String queryCode = this.getRequestParameter("QUERYCODE"); //$NON-NLS-1$
        String code = this.getRequestParameter("CODE"); //$NON-NLS-1$
        String codeName = this.getRequestParameter("CODENAME"); //$NON-NLS-1$
        if("default".equals(queryCode)){ //$NON-NLS-1$
            this.addInfo(Info.ERROR, Messages.getString(this.getLanguage(), "defaultcantbechange")); //$NON-NLS-1$
            return false;
        }
        if(FuncString.isBlank(queryCode)){
            EOABHouseKeeperSpendType chk = new EOABHouseKeeperSpendType();
            chk.setSpendType(code).setName(codeName).setOperator(this.getCurrentUser().getId()); //$NON-NLS-1$
            if(chk.isExist()){
                this.addInfo(Info.ERROR, Messages.getString(this.getLanguage(), "typeisexists")); //$NON-NLS-1$
                return false;
            }
            else {
                chk.save();
                this.addInfo(Info.SUCCESS, code);
                return true;
            }
        }
        else {
             if(!queryCode.equals(code)) {
                 this.addInfo(Info.ERROR, Messages.getString(this.getLanguage(), "spendcodecantbechange")); //$NON-NLS-1$
                 return false;
             }
             String sql = "update ABHouseKeeperSpendType set name=? where spendtype=? and operator=?"; //$NON-NLS-1$
             String sql2 = "update ABHouseKeeperDetail set spendname=? where spendtype=? and operator=?"; //$NON-NLS-1$
             DBSession s = DBSessionFactory.getSession();
             s.execute(sql, codeName, code, this.getCurrentUser().getId());
             s.execute(sql2, codeName, code, this.getCurrentUser().getId());
             s.commit();
             return true;
        }
    }
    
    public boolean deleteSpendType(){
        String code = this.getRequestParameter("CODE"); //$NON-NLS-1$
        if("default".equals(code)){ //$NON-NLS-1$
            this.addInfo(Info.ERROR, Messages.getString(this.getLanguage(), "defaultcantdelete")); //$NON-NLS-1$
            return false;
        }
        String del = "delete from ABHouseKeeperSpendType where operator=? and spendtype=?"; //$NON-NLS-1$
        String upd = "update ABHouseKeeperDetail set spendtype='default', spendname=(select name from ABHouseKeeperSpendType where spendtype='default') where operator=? and spendtype=?";
        String del2 = "delete from ABHouseKeeperTypeItems where item=? and operator=?";
        DBSession session = DBSessionFactory.getSession();
        session.execute(del, this.getCurrentUser().getId(), code);
        session.execute(upd, this.getCurrentUser().getId(), code);
        session.execute(del2, code, this.getCurrentUser().getId());
        session.commit();
        return true;
    }
}
